(function ($) {
	$.courseResource = function () {
		return {	//书本基本信息
			pageSize: 10,
			pageIndex: 0,
			bookStatus: 0,
			pageParam: {},
			init: function(){
				this.listener();
				this.initLayui();
				this.resourceList();
			},
			/**
			 * 添加各按钮所需要的监听事件
			 */
			listener: function(){
                /**
                 *页面调整
                 * **/
                $.courseResource.resize();
                $(window).resize($.courseResource.resize);
				//input content 设置监听
				$("#searchInput").keydown(function(){
					if(event.keyCode==13){
						$.courseResource.pageIndex=0;
						$.courseResource.resourceList();
					}
				});
				$("#searchBtn").click(function(){
					$.courseResource.pageIndex=0;
					$.courseResource.resourceList();
				});
				//点击查看 跳转页面
				$(".resourceList").on("click",".check,.courseName",function(){
					if($(this).parents("tr").attr("data-num")>0){
                        $(parent.document).find("#schoolCoursesRight").attr("src","resourceCourse.html?courseId="+$(this).parents("tr").attr("data-courseid"));
                        //对面包屑进行编辑
                        var html='<a data-href="resourceCourse.html?courseId='+$(this).parents("tr").attr("data-courseid")+'">课程详情</a>';
                        $(parent.document).find(".layui-breadcrumb a:last-child").append('<span class="layui-box">&gt;</span>');
                        $(parent.document).find(".layui-breadcrumb").append(html);
						// window.top.xhTop.createNewTab("课程详情", "../../manage/CA102001/resourceCourse.html?courseId="+$(this).parents("tr").attr("data-courseid"));
					}
				});
			},
            /**
             * 页面设置高度
             * **/
            resize: function () {
                $(".resourceListContent").css("height", window.innerHeight - 126 + "px");
            },
			initLayui: function(){
				layui.use(['layer','form','laypage','table'], function(){
	            	var $ = layui.jquery, layer = (parent.layer === undefined) ? layui.layer : parent.layer, form = layui.form, table = layui.table, laypage = layui.laypage;
					form.on('select', function(data){
						$.courseResource.pageIndex=0;
						$.courseResource.resourceList();
					});
            	});
			},
			resourceList: function(ifPage){
                if (!ifPage) {
                    $.courseResource.pageIndex = 0;
                }
				var submitData = {
					grade: $("select[name=grade]").val(),
					subject: $("select[name=subject]").val(),
					content: $("#searchInput").val(),
					page: $.courseResource.pageIndex,
                    size:$.courseResource.pageSize
				};
				//console.log(submitData)
                updateObject(Doman.url+Doman.outCourseList, true, JSON.stringify(submitData), function (result) {
					$(".totalNum span").html(result.courseDTOPage.totalElements);
					//如果学科和年级列表不为null 则加载其列表
					if(result.gradeDTOList!=null){
						$.courseResource.selectRender(result.gradeDTOList,$("select[name='grade']"));
						$.courseResource.selectRender(result.subjectDTOList,$("select[name='subject']"));
					}
					$.courseResource.courseList(result.courseDTOPage.content);
					$.courseResource.initPage(result.courseDTOPage.totalElements);
               }, function () {
                });
			},
			/**
			 * 列表展示
			 * @data 列表数据
			 */
			courseList:function(data){
				var html='';
				$.each(data,function(i,data){
					html+='<tr data-num='+data.unitNum+' data-courseid='+data.courseid+'>'
						+ '    <td>'+($.courseResource.pageIndex*$.courseResource.pageSize+(i+1))+'</td>'
						+ '    <td><img src='+data.coverImageUrl+'></td>'
						+ '    <td class="courseName">'+data.courseName+'</td>'
						+ '    <td>'+data.edition+'-'+data.grade+'-'+data.subject+'</td>'
						+ '    <td>共'+data.unitNum+'讲</td>'
						+ '    <td>'+data.author+'/'+data.cooperators+'</td>'
						+ '    <td><a class="check">查看</a></td>'
						+ '</tr>';
				});
				$(".resourceList tbody").html(html);
			},
			/**
			 * select列表重置
			 * @list list object
			 * @dom 节点选择
			 */
			selectRender:function(list,dom){
				var html='<option value="0" selected="">全部</option>';
				$.each(list,function(i,data){
					html+=' <option value='+data.id+'>'+data.name+'</option>'
				});
				dom.html(html);
				layui.use('form', function(){
					var form = layui.form;
					form.render('select');
				});
			},
			/**
			 * 初始化分页页码
			 * @returns
			 */
			initPage: function(total){
				layui.use(['laypage'], function(){
					$ = layui.jquery, layer =(parent.layer === undefined) ? layui.layer : parent.layer, laypage = layui.laypage;
					laypage.render({
						elem: 'pagination'
						,count: total //数据总数，从服务端得到
						,first: '首页'
						,last: '尾页'
						,layout: [ 'prev', 'page', 'next', 'skip','limit']
						,curr: $.courseResource.pageIndex+1
						,limit: $.courseResource.pageSize
						,skip: true
						,theme: '#337ab7'
						,jump: function(obj, first){
							$.courseResource.pageIndex = obj.curr-1;
							$.courseResource.pageSize = obj.limit;
							//首次不执行
							if(!first){
								$.courseResource.resourceList(true);
							}
						}
					});
				});
			}
		}
	}();
    $.courseResource.init();
})(jQuery);